<?php
include('config.php');
//获得手机号
//$user_model = array('13333333333','15010443859');

//查询到店人数
$count_user = $mysql->selectCount('guest');
//查询相关的用户
$all_user = $mysql->selectList('guest','',"guest_id desc ",10);
$array = array();
foreach($all_user as $k=>$v){
	$array[$k] = $v;
}
//查询到店顾客
$guest_sql = "select count(*) num from guest";
$guest_rs = $mysql->execute($guest_sql);
$guest_row = mysql_fetch_assoc($guest_rs);
//查询商品销售情况
$month = isset($_GET['month']) ? intval($_GET['month']) : 0;
if($month){
	$start_time = time() - $month*30*3600*24;
}else{
	$start_time = 0;
}

$order = isset($_GET['order']) ? intval($_GET['order']) :0;
$desc = "";
switch ($order) {
	case '1':
		$desc = 'number_name';
		break;
	case '2':
		$desc = 'num';
		break;
	case '3':
		$desc = 'all_price';
		break;
	default:
		$desc = 'number_name';
}

$sql1 = "select number_id,sum(buy_goods_num) num,sum(all_price) all_price,number_name 
		from (SELECT l.* ,(`buy_goods_num` *  `buy_goods_price`)all_price, 
			n.number_name FROM buy_goods_log l LEFT JOIN number n 
			ON l.number_id = n.number_id WHERE l.buy_goods_date >{$start_time} ) v 
		group by number_id ORDER BY {$desc} DESC limit 0,10";
$rs1 = $mysql->execute($sql1);
$good_arr = array();
while($row = mysql_fetch_assoc($rs1)){
	$good_arr[] = $row;
}
//求销售价格

$price_sql = "SELECT sum(buy_goods_num*buy_goods_price) price FROM `buy_goods_log` WHERE 1";
$price_rs = $mysql->execute($price_sql);
$price_arr = mysql_fetch_assoc($price_rs);

//求警戒线以下的产品
$caution_line = $mysql->getOne('caution_line','1');
$caution_sql = "SELECT stock_num, breed_name, number_name FROM  `stock` s LEFT JOIN number n ON s.number_id = n.number_id 
LEFT JOIN breed b ON s.breed_id = b.breed_id WHERE stock_num<{$caution_line[0]} LIMIT 0 , 10";
$caution_rs = $mysql->execute($caution_sql);
$caution_array=array();
while ($caution_row = mysql_fetch_assoc($caution_rs)) {
	$caution_array[] = $caution_row;
}
//求警戒数量
$caution_num_sql = "select count(*) num from stock where stock_num<{$caution_line[0]} ";
$caution_num_rs = $mysql->execute($caution_num_sql);
$caution_num_arr = mysql_fetch_assoc($caution_num_rs);

//$caution_array = $mysql->selectList('stock',"stock_num<{$caution_line[0]}",'stock_num','10');

//超过警戒线数量
$smarty->assign('caution_num',$caution_num_arr['num']);
//警戒产品
if($caution_array){
	$smarty->assign('caution_arr',$caution_array);
}

$smarty->assign('guest_num',$guest_row['num']);
$smarty->assign('price',$price_arr['price']);

$smarty->assign('user_arr', $array);
$smarty->assign('good_arr', $good_arr);

$smarty->assign('link', 1);

$smarty->display('main.html');